package com.simple.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.simple.entity.SysMenu;
import com.simple.entity.vo.SysMenuVo;
import com.simple.repository.SysMenuRepository;
import com.simple.service.SysMenuService;
import com.simple.utils.SysConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    private static final Logger logger = LoggerFactory.getLogger(SysMenuServiceImpl.class);

    @Autowired
    private SysMenuRepository sysMenuRepository;

    @Override
    public SysMenu findByMenuId(Integer id) {
        return sysMenuRepository.findByMenuId(id);
    }

    @Override
    public List<SysMenu> findByMenuIds(String[] ids) {
        return sysMenuRepository.findByMenuIds(ids);
    }

    @Override
    public List<SysMenu> findByMenuIdsStatus(String[] ids) {
        return sysMenuRepository.findByMenuIdsStatus(ids);
    }

    @Override
    public List<SysMenu> findAll() {
        List<SysMenu> list=new ArrayList<>();
        List<SysMenu> twoList=sysMenuRepository.findByLevel(2);
        if(twoList.size()>0) {
        	 for (int i = 0; i < twoList.size(); i++) {
                 SysMenu sysMenuLevelTwo=twoList.get(i);
                 list.add(sysMenuLevelTwo);
                 List<SysMenu> sysMenu=sysMenuRepository.findByParentId(sysMenuLevelTwo.getId());
                 for (int j = 0; j < sysMenu.size(); j++) {
                 	SysMenu sysMenuLevelThree=sysMenu.get(j);
                 	list.add(sysMenu.get(j));
                 	List<SysMenu> threeList=sysMenuRepository.findByParentId(sysMenuLevelThree.getId());
                 	for (int k = 0; k < threeList.size(); k++) {
                 		list.add(threeList.get(k));
     				}
                 }
             }
        }
        return list;
    }

    @Override
    public List<SysMenu> findByLevel(Integer level) {
        return sysMenuRepository.findByLevel(level);
    }

    @Override
    public List<SysMenu> findByParentId(Integer id) {
        return sysMenuRepository.findByParentId(id);
    }

    @Override
    public List<Object> findMenuTree(Integer startLevel,Integer toplevel) {
        List<SysMenu> sysMenuList=sysMenuRepository.findByLevel(startLevel);//除去顶级栏目的一级栏目
        List<Object> list = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            JSONObject obj = new JSONObject(true);
            obj.put("id", sysMenu.getId());
            obj.put("title", sysMenu.getMenuName());
            obj.put("menuLevel", sysMenu.getMenuLevel());
            obj.put("menuAddress", sysMenu.getMenuAddress());
            obj.put("children", getChildren(sysMenu.getId(),toplevel));
            list.add(obj);
        }
        return list;
    }

    public List<Object> getChildren(Integer parentId,Integer level){
        List<Object> list = new ArrayList<>();
        List<SysMenu> sysMenuList=sysMenuRepository.findByParentId(parentId);
        for (SysMenu sysMenu : sysMenuList) {
            JSONObject obj = new JSONObject(true);
            obj.put("id", sysMenu.getId());
            obj.put("title", sysMenu.getMenuName());
            obj.put("menuLevel", sysMenu.getMenuLevel());
            obj.put("menuAddress", sysMenu.getMenuAddress());
            if(sysMenu.getMenuLevel()!=level) {//3级为最低的一级
                obj.put("children", getChildren(sysMenu.getId(),level));
            }
            list.add(obj);
        }
        return list;
    }

    @Override
    public List<Object> findSecondLevel(int[] ids) {
        List<Object> list=sysMenuRepository.findSecondLevel(ids);
        return list;
    }

    @Override
    public List<SysMenu> findByMenuIdsGroup(String[] ids) {
        return sysMenuRepository.findByMenuIdsGroup(ids);
    }

    @Override
    public List<SysMenu> findByInParentId(String[] ids, Integer parentId) {
        return sysMenuRepository.findByInParentId(ids, parentId);
    }

    @Override
    public void saveAndFlush(SysMenu sysMenu) {
        sysMenuRepository.saveAndFlush(sysMenu);
        SysConfig.removeMenuCache();
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        sysMenuRepository.deleteById(id);
    }

	@Override
	public SysMenuVo findByMenuIdVo(Integer id) {
		List<SysMenuVo> list=sysMenuRepository.findByMenuIdVo(id);
		if(list.size()>0) {
			return list.get(0);
		}
		return null;
	}
	
}
